package org.fig.ioc.overview.dependency.injection;

import org.fig.ioc.overview.annotataion.Super;
import org.fig.ioc.overview.domain.User;
import org.fig.ioc.overview.repository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.AbstractRefreshableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Map;

/**
 * @Description:
 * @Author: Flynn Vanderboom
 * @Data: 2021/6/8 22:57
 */

public class DependencyInjectionDemo {
    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-injection-context.xml");
/*        lookupRealTime(beanFactory);
        lookupByType(beanFactory);*/
     /*   lookupByType(beanFactory);*/
/*        lookupByCollectionType(beanFactory);
        lookupByAnnotation(beanFactory);*/


        UserRepository userRepository = beanFactory.getBean(UserRepository.class);
        System.out.println(userRepository.getBeanFactory() == beanFactory);
        
        //依赖注入 (内建依赖)
        System.out.println(userRepository.getBeanFactory());
        System.out.println();


/*        Object object1 = new Object();
        Object object2 =object1;
        Object object3 = object1;
        System.out.println("测试结果:"+(object2==object3));*/

/*

        //依赖查找
        System.out.println(beanFactory.getBean(BeanFactory.class));*/
        
        ObjectFactory objectFactory = userRepository.getObjectFactory();
        System.out.println(objectFactory.getObject()==beanFactory);
        




        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-injection-context.xml");
        UserRepository userRepository2 = applicationContext.getBean(UserRepository.class);
        System.out.println(applicationContext.getAutowireCapableBeanFactory());
        System.out.println("这两个BeanFactory:"+( userRepository2.getBeanFactory() == applicationContext.getAutowireCapableBeanFactory()));



        //容器内建Bean
        Environment environment = beanFactory.getBean(Environment.class);
        System.out.println(environment);
    }


    private static void lookupByAnnotation(BeanFactory beanFactory){
        if(beanFactory instanceof ListableBeanFactory){
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String,User> users = (Map) listableBeanFactory.getBeansWithAnnotation(Super.class);
            System.out.println("查找到所有的标注@Super的集合对象:"+users);
        }
    }

    private static void lookupByCollectionType(BeanFactory beanFactory){
        if(beanFactory instanceof ListableBeanFactory){
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String,User> users = listableBeanFactory.getBeansOfType(User.class);
            System.out.println("查找到所有的User的集合对象:"+users);
        }
    }

    private static void lookupByType(BeanFactory beanFactory){
        UserRepository userRepository = beanFactory.getBean(UserRepository.class);
        System.out.println("按类型实时查找:"+userRepository);
    }




    private static void lookupInLazy(BeanFactory beanFactory) {
        ObjectFactory<User> objectFactory = (ObjectFactory) beanFactory.getBean("objectFactory");
        User user = objectFactory.getObject();
        System.out.println("延迟查找:" + user);
    }


    private static void lookupRealTime(BeanFactory beanFactory) {
        User user = (User) beanFactory.getBean("user");
        System.out.println("实时查找:" + user);
    }

}
